<?php

namespace App\Http\Controllers\Admin;

use App\Model\Article;
use App\Model\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $url = Storage::disk('public')->url('photo/2020-04-27-23-12-07.jpg');
        $listkey = 'list:article';
        $hashkey = 'hash:article:';

        if(Redis::exists($listkey)){
            //获取文章ID
            $articleid = Redis::lrange($listkey,0,-1);
        }else{
            $article = Article::paginate(10);
            //保存数据到redis

        }


        return view('admin.article.list', compact('article'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取栏目
        $cate = Category::all();
        return view('admin.article.art_add', compact('cate'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->except('_token');
        //验证省略
        $article = new Article();
//        dd($input);
        $article->art_title = $input['art_title'];
        $article->art_tag = isset($input['art_tag'])?$input['art_tag']:'';
        $article->art_description = $input['art_description'];
        $article->art_thumb = isset($input['art_thumb'])?implode(',',$input['art_thumb']):'';//处理缩略图
        $article->art_content = $input['art_content'];
        $article->art_time = time();
        $article->art_editor = isset($input['art_editor'])?$input['art_editor']:'匿名';
        $article->cate_id = $input['cate_id'];
        $status = $article->save();
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'添加成功'
            ];

        }else{
            $res = [
                'status'=>1,
                'data'=>['添加失败！'],
                'message'=>'添加失败'
            ];
        }
        return json_encode($res);
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function imgUpload(Request $request)
    {

        $file = $request->file('file');
        if ($file->isValid()) {
            //获取上传文件的后缀
            $ext = $file->getClientOriginalExtension();
            //重命名
            $filename = date('Y-m-d-H-i-s') . '.' . $ext;
            $store_result = $file->storeAs('photo',$filename,'public');
            $rs = [
                "code" => 0,
                'msg' => '上传成功',
                'data' => [
                    "src" => $store_result
                ]
            ];
        } else {
            $rs = [
                "code" => 1,
                'msg' => '上传失败',
                'data' => [
                    "src" => ""
                ]
            ];

        }
        return json_encode($rs);
    }
}
